﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tea;

namespace AliYun_RenLian_Obj
{
    internal class Sample_Add_RenLian_YangBen
    {
        /// <term><b>Description:</b></term>
        /// <description>
        /// <para>使用AK&amp;SK初始化账号Client</para>
        /// </description>
        /// 
        /// <returns>
        /// Client
        /// </returns>
        /// 
        /// <term><b>Exception:</b></term>
        /// Exception
        public static AlibabaCloud.SDK.Facebody20191230.Client CreateClient()
        {
            // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
            // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378671.html。
            //AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            //{
            //    // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
            //    AccessKeyId = Environment.GetEnvironmentVariable("LTAI5tA1goPxJXZV3urxxig9"),
            //    // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
            //    AccessKeySecret = Environment.GetEnvironmentVariable("Yxiz5MW4NSx6zjjuiTOov0EhiXeccm"),
            //};
            string AccessKeyId = System.Configuration.ConfigurationManager.ConnectionStrings["RenLian_AccessKeyId"].ConnectionString.ToString();
            string AccessKeySecret = System.Configuration.ConfigurationManager.ConnectionStrings["RenLian_AccessKeySecret"].ConnectionString.ToString();

            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                AccessKeyId = AccessKeyId,
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                AccessKeySecret = AccessKeySecret,
            };
            // Endpoint 请参考 https://api.aliyun.com/product/facebody
            config.Endpoint = "facebody.cn-shanghai.aliyuncs.com";
            return new AlibabaCloud.SDK.Facebody20191230.Client(config);
        }
       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="RenLianDb_Name">数据库名称</param>
        /// <param name="EntityId">实体id</param>
        /// <param name="Labels">学员名称</param>
        /// <returns></returns>
        public Res_msg Main2(string RenLianDb_Name, string EntityId,string Labels)
        {
            try
            {
 Res_msg res_Msg = new Res_msg();
            res_Msg.code = 1;
            AlibabaCloud.SDK.Facebody20191230.Client client = CreateClient();
            AlibabaCloud.SDK.Facebody20191230.Models.AddFaceEntityRequest addFaceEntityRequest = new AlibabaCloud.SDK.Facebody20191230.Models.AddFaceEntityRequest
            {
                DbName = RenLianDb_Name,
                EntityId = EntityId,
                Labels = Labels,
            };
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            try
            {
                string Confidence = System.Configuration.ConfigurationManager.ConnectionStrings["Confidence"].ConnectionString.ToString();

                //AlibabaCloud.SDK.Facebody20191230.Models.CompareFaceResponse resp = client.CompareFaceWithOptions(compareFaceRequest, runtime);
                // 复制代码运行请自行打印 API 的返回值
                var resp = client.AddFaceEntityWithOptions(addFaceEntityRequest, runtime);
                
                    res_Msg.Message ="实体：【"+ EntityId+"】标签：【"+Labels+"】在数据库【"+ RenLianDb_Name+"】中创建成功！";
                    return res_Msg;
                //AlibabaCloud.TeaConsole.Client.Log(AlibabaCloud.TeaUtil.Common.ToJSONString(resp));
            }
            catch (TeaException error)
            {
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Console.WriteLine(error.Message);
                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
                res_Msg.code = 0;
                if (error.Message.Contains("entity has existed"))
                {
                    res_Msg.Message = "实体：【" + EntityId + "】标签：【" + Labels + "】在数据库【" + RenLianDb_Name + "】中创建失败！实体已存在,无需重复创建实体！";
                }
                else {
                    res_Msg.Message = error.Message;
                }

                    return res_Msg;
            }
            catch (Exception _error)
            {
                TeaException error = new TeaException(new Dictionary<string, object>
                {
                    { "message", _error.Message }
                });
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Console.WriteLine(error.Message);
                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
                res_Msg.code = 0;
                res_Msg.Message = error.Message;
                return res_Msg;
            }
            }
            catch (Exception)
            {
                Res_msg res_Msg = new Res_msg();
                res_Msg.code = 0;
                return res_Msg;
            }
           
        }

    }
}
